package com.lechi.web.controller;

import com.lechi.iot.netty.utils.NettyCacheUtils;
import com.lechi.web.common.core.controller.BaseController;
import com.lechi.web.common.core.domain.AjaxResult;
import com.lechi.web.common.core.page.TableDataInfo;
import com.lechi.web.common.utils.poi.ExcelUtil;
import com.lechi.web.service.IIotAlertRecordService;
import com.lechi.web.system.domain.IotAlertRecord;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 告警列表Controller
 *
 * @author yangguang
 * @date 2024 -05-10
 */
@Slf4j
@RestController
@RequestMapping("/iot/record")
@RequiredArgsConstructor
public class IotAlertRecordController extends BaseController {

    private final IIotAlertRecordService iotAlertRecordService;

    /**
     * 查询告警列表列表
     *
     * @param iotAlertRecord the iot alert record
     * @return the table data info
     */
    @GetMapping("/list")
//    @PreAuthorize("@ss.hasPermi('iot:record:query')")
    public TableDataInfo list(IotAlertRecord iotAlertRecord) {
        startPage();
        List<IotAlertRecord> list = iotAlertRecordService.selectIotAlertRecordList(iotAlertRecord);
        return getDataTable(list);
    }

    /**
     * 导出告警列表列表
     *
     * @param response       the response
     * @param iotAlertRecord the iot alert record
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, IotAlertRecord iotAlertRecord) {
        List<IotAlertRecord> list = iotAlertRecordService.selectIotAlertRecordList(iotAlertRecord);
        ExcelUtil<IotAlertRecord> util = new ExcelUtil<>(IotAlertRecord.class);
        util.exportExcel(response, list, "告警列表数据");
    }

    /**
     * 获取告警列表详细信息
     *
     * @param id the id
     * @return the info
     */
    @GetMapping(value = "/{id}")
//    @PreAuthorize("@ss.hasPermi('iot:record:query')")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(iotAlertRecordService.selectIotAlertRecordById(id));
    }

    /**
     * 新增告警列表
     *
     * @param iotAlertRecord the iot alert record
     * @return the ajax result
     */
    @PostMapping("/add")
//    @PreAuthorize("@ss.hasPermi('iot:record:add')")
    public AjaxResult add(@RequestBody IotAlertRecord iotAlertRecord) {
        return toAjax(iotAlertRecordService.insertIotAlertRecord(iotAlertRecord));
    }

    /**
     * 修改告警列表
     *
     * @param iotAlertRecord the iot alert record
     * @return the ajax result
     */
    @PostMapping("/edit")
//    @PreAuthorize("@ss.hasPermi('iot:record:edit')")
    public AjaxResult edit(@RequestBody IotAlertRecord iotAlertRecord) {
        return toAjax(iotAlertRecordService.updateIotAlertRecord(iotAlertRecord));
    }

    /**
     * 删除告警列表
     *
     * @param ids the ids
     * @return the ajax result
     */
    @DeleteMapping("/{ids}")
//    @PreAuthorize("@ss.hasPermi('iot:record:remove')")
    public AjaxResult remove(@PathVariable Long[] ids) {
        Arrays.stream(ids).forEach(id -> {
            final IotAlertRecord iotAlertRecord = iotAlertRecordService.selectIotAlertRecordById(id);
            NettyCacheUtils.remove("AlarmRuleType_" + iotAlertRecord.getDeviceId() + "_" + iotAlertRecord.getSensorAliasId());
        });
        return toAjax(iotAlertRecordService.deleteIotAlertRecordByIds(ids));
    }
}
